package com.yztc.orangeentertainment.ui.main.subscribefragment.staritem.starnewsfragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yztc.orangeentertainment.R;
import com.yztc.orangeentertainment.adapter.StarNewsInfoAdapter;
import com.yztc.orangeentertainment.base.BaseFragment;
import com.yztc.orangeentertainment.base.OnItemClickListener;
import com.yztc.orangeentertainment.bean.channalbean.ListBean;
import com.yztc.orangeentertainment.config.SubscribeUrlConfig;
import com.yztc.orangeentertainment.ui.main.channalarticle.ChannalArticleActivity;
import com.yztc.orangeentertainment.utils.DividerItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by My on 2016/10/7.
 * 资讯
 */
public class NewsFrament extends BaseFragment implements NewsContract.View {
    @InjectView(R.id.rv_starnews)
    RecyclerView mRecyclerView;

    private NewsPresenter presenter;
    private int page = 1;
    private String sid;
    private LinearLayoutManager linearLayoutManager;
    private StarNewsInfoAdapter adapter;
    private int lastVisibleItem;
    private List<ListBean> totalList=new ArrayList<>();
    @Override
    public int getLayoutId() {
        return R.layout.fragment_starnews;
    }

    /**
     * 获取list集合
     * @param listBeen
     */
    @Override
    public void onLoadStarSuccess(List<ListBean> listBeen) {
        totalList.addAll(listBeen);
        adapter.setListBean(totalList);
        if (page==1){
            mRecyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        adapter.setListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent=new Intent(getActivity(), ChannalArticleActivity.class);
                intent.putExtra("id",totalList.get(position).getId()+"");
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    @Override
    public void onLoadStarFail(String msg) {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.inject(this, rootView);
        initView();
        initPresenter();
        return rootView;
    }

    /**
     * 初始化视图
     */
    private void initView() {
        adapter = new StarNewsInfoAdapter(getActivity());
        sid = getArguments().getString("sid");
        linearLayoutManager = new LinearLayoutManager(getActivity());

        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //间距
        mRecyclerView.addItemDecoration(new DividerItemDecoration(20));
        mRecyclerView.setLayoutManager(linearLayoutManager);

        //上拉加载
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && (lastVisibleItem + 1) == adapter
                        .getItemCount()) {
                    page++;

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {

                            initPresenter();
                            adapter.notifyDataSetChanged();

                        }
                    }, 2000);
                }
            }
        });
    }

    /**
     * 初始化present
     */
    private void initPresenter() {
        presenter = new NewsPresenter(this);
        Map<String, String> map = new HashMap<>();
        //传入参数
        map.put(SubscribeUrlConfig.Params.SID, sid);
        map.put(SubscribeUrlConfig.Params.PAGE, page + "");
        map.put(SubscribeUrlConfig.Params.TS, SubscribeUrlConfig.All_DefaultVaule.TS_VALUE);
        map.put(SubscribeUrlConfig.Params.MAC, SubscribeUrlConfig.All_DefaultVaule.MAC_VALUE);
        map.put(SubscribeUrlConfig.Params.UID, SubscribeUrlConfig.All_DefaultVaule.UID_VALUE);
        map.put(SubscribeUrlConfig.Params.PF, SubscribeUrlConfig.All_DefaultVaule.PF_VALUE);
        map.put(SubscribeUrlConfig.Params.NET, SubscribeUrlConfig.All_DefaultVaule.NET_VALUE);
        map.put(SubscribeUrlConfig.Params.ACCESSTOKEN, SubscribeUrlConfig.All_DefaultVaule.ACCESSTOKEN_VALUE);
        map.put(SubscribeUrlConfig.Params.CHANNEL, SubscribeUrlConfig.All_DefaultVaule.CHANNEL_VALUE);
        map.put(SubscribeUrlConfig.Params.VER, SubscribeUrlConfig.All_DefaultVaule.VER_VALUE);
        map.put(SubscribeUrlConfig.Params.RES, SubscribeUrlConfig.All_DefaultVaule.RES_VALUE);
        presenter.getStarFromNet(map, getActivity());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
